package com.entitymapper.field.impl;

import java.lang.reflect.Field;

import org.apache.log4j.Logger;

import com.entitymapper.EntityMapperException;
import com.entitymapper.annotation.rfid.RFIDMapping;
import com.entitymapper.driver.Format;
import com.entitymapper.driver.impl.RFIDFormat;

/**
 * 解释实体对象类上关于读写规则的注解操作
 * 通过实体对象与实体对象对应的属性名
 * 返回该实体对象的读写规则
 * @author hhf
 */
public class CheckRFIDMapping {
	
	private static final Logger log = Logger.getLogger(CheckRFIDMapping.class);
	
	/**
	 * 解释实体对象类上关于读写规则的注解操作
	 * @param entityType 实体对象
	 * @param fieldName 实体对象对应的属性名
	 * @return 实体对象的读写规则
	 * @throws entityMapperException
	 */
	public static Format chectFieldName(Class<?> entityType, String fieldName) throws EntityMapperException{
		Field field = null;
		RFIDMapping rfidMapping = null;
		try {
			field = entityType.getDeclaredField(fieldName);
			rfidMapping = field.getAnnotation(RFIDMapping.class);
		} catch (Exception e) {
			log.error(e);
			throw new EntityMapperException("the "+field.getName()+" has not Annotation!",e);
		}
		if(null == rfidMapping) {
			throw new EntityMapperException("the "+field.getName()+" has not Annotation!");
		}
			
		int count = rfidMapping.areaCount();
		int start = rfidMapping.areaStarts();
		RFIDFormat format = new RFIDFormat();
		format.setAreaCount(count);
		format.setAreaStart(start);
		return format;
	}
}
